package com.org.web.support.config;

import com.org.platform.support.conponents.Web;
import com.org.web.support.interceptors.AccessInterceptor;
import com.org.web.support.interceptors.ApiInterceptor;
import com.org.web.support.interceptors.LimitInterceptor;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.Codec;
import org.redisson.client.codec.StringCodec;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author devg @Date 2020/2/26 18:51
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

	@Autowired
	private Web web;

	@Autowired
	private AccessInterceptor accessInterceptor;

	@Autowired
	private LimitInterceptor limitInterceptor;

	@Autowired
	private ApiInterceptor apiInterceptor;

	private static final String[] apiInterceptorPatterns = {"/api/**", "/mobile/unishop/**"};

	private static final String[] apiInterceptorExcludePatterns = {"/api/admin/index/login", "/mobile/unishop/login", "/mobile/unishop/test"};

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		String[] filterUrlsArray = web.getFilterUrls();
		String[] noFilterUrlsArray = web.getNoFilterUrls();
//    registry.addInterceptor(limitInterceptor);
		registry.addInterceptor(accessInterceptor).addPathPatterns(filterUrlsArray).excludePathPatterns(noFilterUrlsArray);
		registry.addInterceptor(apiInterceptor).addPathPatterns(apiInterceptorPatterns).excludePathPatterns(apiInterceptorExcludePatterns);
	}

	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**")
				.allowedOrigins("*")
				.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
				.allowedHeaders("*") // 允许任何请求头
				.allowCredentials(true) // 允许证书、cookie
				.exposedHeaders(HttpHeaders.SET_COOKIE)
				.maxAge(3600);
	}

  /*@Bean
  public HttpMessageConverters fastJsonConfigure() {
    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(
//        SerializerFeature.PrettyFormat,
        SerializerFeature.WriteMapNullValue,
        SerializerFeature.WriteNullStringAsEmpty,
        SerializerFeature.WriteNullListAsEmpty,
        SerializerFeature.WriteNullNumberAsZero,
        SerializerFeature.WriteNullBooleanAsFalse
    );
    //中文乱码解决方案
    List<MediaType> fastMedisTypes = new ArrayList<>();
    //设定Json格式且编码为utf-8
    fastMedisTypes.add(MediaType.APPLICATION_JSON_UTF8);
    fastConverter.setSupportedMediaTypes(fastMedisTypes);
    fastConverter.setFastJsonConfig(fastJsonConfig);
    return new HttpMessageConverters(fastConverter);
  }*/

	@Bean(name = "redissonClient", destroyMethod = "shutdown")
	public RedissonClient redissonClient() {
		Config config = new Config();
		config.useSingleServer().setAddress("redis://127.0.0.1:6379").setPassword("redis").setDatabase(0);
		Codec codec = new StringCodec();
		config.setCodec(codec);
		RedissonClient redissonClient = Redisson.create(config);
		return redissonClient;
	}

}
